<?php
class CScore
{
	public function getTotal()
	{
		$member = new CMember();
    	$db = $member->getAdapter();
    	$result = $db->fetchAll('select sum(integral) as total from ' . R_PRE . 'members where member_type="buyer"');
    	return $result;
	}
	
	public function registerScore($type = 'score')
	{
		$config = new CConfig();
		$score = $config->getByName('REGISTER_SCORE')->data;
		$scoreArr = explode('|', $score);
		if ($type == 'score') {
			return $scoreArr[0];
		} else {
			return $scoreArr[1];
		}
	}
	
	public function orderScore($type = 'score')
	{
		$config = new CConfig();
		$score = $config->getByName('ORDER_SCORE')->data;
		$scoreArr = explode('|', $score);
		if ($type == 'score') {
			return $scoreArr[0];
		} else {
			return $scoreArr[1];
		}
	}
	
	public function dollar2score($dollar)
	{
		$score = $this->orderScore('score');
		$scoreResult = $dollar * $score;
		return $scoreResult;
	}
	
	public function updateIntegralByDollar($dollar, $uid)
	{
		$statu = $this->orderScore('statu');
		if ($statu == 1) {
			$member = new CMember();
			$dollar = $dollar / ORDER_DOLLAR;
			$score = $this->dollar2score($dollar);
			return $member->updateIntegral($score, $uid, 1);
			
		}
		return true;
	}
	
	public function score2dollar($userScore)
	{
		$score = $this->orderScore('score');
		$dollarResult = number_format(round(($userScore / $score) * 100)/100,2, '.', '');
		return $dollarResult;
	}
	
	public function addCart($items)
	{
		setcookie(COOKIE_PRE.'product_buy_session', '', time()-1000,'/');
		$sessionHash = $_COOKIE[COOKIE_PRE.'product_buy_session'];
		$cart = new CCart();
        $shoppingArray=array();
        $sessionHash = '';
        if($sessionHash != '')
        {
			$cartStr = $cart->getCartStr($sessionHash);
			if ($cartStr) {
           		$shoppingArray=json_decode(stripslashes($cartStr),true);			}			
        } else {
        	$sessionHash = $cart->sessionHash();
        }
		$itemArr = explode(',', $items);
		$price=new CProductPrice();
		$product = new CProduct();
		foreach ($itemArr as $v) {
			if ($product->getDetail($v)) {
				$priceArray=$price->getAllPrice($v);  
				if ($pirceArray) {
					$patternName = $priceArray[0]['pattern_name'];
				} else {
					$patternName = 'item';
				}
				$cookieArray[$v] = array('pattern_name'=>$patternName, 'counts'=>1, 'pattern_size'=>'L:1',
										 'printMesg'=>'Size:L,Amount:1,BadgePatch: no |', 
										 'badge_patch_amount'=>0, 'BadgePatch'=>'No', 
										 'originalname_num'=>'' ,'originalprintAmounts'=>'',
										 'name_num'=>'', 'printAmounts'=>'');
	            $shoppingArray[$v]=$cookieArray[$v];
			}
		}       
        $cartStr = json_encode($shoppingArray);
		$row = array('cart_str'=>addslashes($cartStr), 'expires'=>time()+3600*24*180);
        setcookie(COOKIE_PRE.'product_buy_session', $sessionHash, time()+3600*24*180,'/');
        setcookie(COOKIE_PRE.'product_coupon_id', $items, time()+3600*24*180,'/');
		$cart->setCartStr($sessionHash, $row);
	}
}